local BaseSprite = import('..views.BaseSprite')

MatrixMap = MatrixMap or {}


--[[
随机创建矩阵
]]
function MatrixMap.create(tager)
	local self = tager
	for row=1,9 do
		for col=1,8 do
			if baseMap[row][col] == 1 and baseWall[row][col] == 0 then 									--随机精灵
				local file = SPRITE_SOURCES[math.random(1,5)]
				local tmpSprite = BaseSprite.new({filename=file,row=row,column=col}):addTo(self)
				tmpSprite:setScale(0.5)
				gSprites[row] = gSprites[row] or {}
				gSprites[row][col] = tmpSprite
				tmpSprite:retain()
				
			elseif baseWall[row][col] == 1 then 														--产生墙
				local file = 'wall.png'
				local tmpSprite = BaseSprite.new({filename=file,row=row,column=col}):addTo(self)
				tmpSprite:setScale(0.5)
				gWallSprites[row] = gWallSprites[row] or {}
				gWallSprites[row][col] = tmpSprite
				tmpSprite:retain()
			end
			if baseMap[row][col] == 1 then
				local x,y = BaseSprite.toPosition(col,row)
				local back=display.newSprite('#backItem.png',x,y):addTo(self)
				back:setScale(0.5)
				back:setOpacity(128)
			end
		end
	end
end

--[[
判断矩阵里是否有可消除对象，如果有则返回false，需要修正
]]
function MatrixMap.checkGSprites(tager)
	local self = tager
	for row=1,9 do
		for col=1,8 do
			if baseMap[row][col] == 1 and baseWall[row][col] == 0 then
				local sprite = gSprites[row][col]
				if sprite then
					local h,v = sprite:judgeItem()
					if h and h>2 or v and v>2 then
						return false
					end
				end
			end
		end
	end
	return true
end 

--[[
修正，查找内部直接可消除的点，并返回替换表{frome={},to={}}
]]
gMaxtrixByRandomNumber = 0
function MatrixMap.reviseRandomGSprites(tager)
	local originMatrix = {}
	local type1Matrix = {}
	local type2Matrix = {}
	local type3Matrix = {}
	local type4Matrix = {}
	local type10Matrix = {}
	gMaxtrixByRandomNumber = gMaxtrixByRandomNumber + 1
	printf('gMaxtrixByRandomNumber=%d',gMaxtrixByRandomNumber)
	--生成原矩阵
	function createOriginMatrix() 
		local matrix = {}
		for row=1,9 do
			for col=1,8 do
				if baseMap[row][col] == 0 or baseWall[row][col]==1 then
					matrix[row] = matrix[row] or {}
					matrix[row][col] = 0
				elseif gSprites[row][col] then
					matrix[row] = matrix[row] or {}
					matrix[row][col] = gSprites[row][col].sytle
				else
					matrix[row] = matrix[row] or {}
					matrix[row][col] = 0
				end
			end
		end
		return matrix
	end

	originMatrix = createOriginMatrix()

	--判断矩阵
	function judgeSytle(_col,_row,_sytle,_matrix)
		local col,row,sytle = _col,_row,_sytle
		local matrix = _matrix
		local h,v = 0,0
		local hp,vp = {col,col},{row,row}
		--1
		for i=col+1,8 do
			if matrix[row][i] == sytle then
				h = h + 1
				hp[2] = i
			else
				break
			end
		end
		for i=col-1,1,-1 do
			if matrix[row][i] == sytle then
				h = h + 1
				hp[1] = i
			else
				break
			end
		end

		--2
		for i=row+1,9 do
			if matrix[i][col] == sytle then
				v = v + 1
				vp[2] = i
			else
				break
			end
		end
		for i=row-1,1,-1 do
			if matrix[i][col] == sytle then
				v = v + 1
				vp[1] = i
			else
				break
			end
		end
		return {h,v,hp,vp}
	end

	--生成各类形相同数量的矩阵
	function createSameNumberMatrix(_matrix)
		local matrix = _matrix
		local m1,m2,m3,m4,m5 = {},{},{},{},{}

		for row=1,9 do
			for col=1,8 do
				m1[row] = m1[row] or {}
				m1[row][col] = judgeSytle(col,row,1,matrix)

				m2[row] = m2[row] or {}
				m2[row][col] = judgeSytle(col,row,2,matrix)

				m3[row] = m3[row] or {}
				m3[row][col] = judgeSytle(col,row,3,matrix)

				m4[row] = m4[row] or {}
				m4[row][col] = judgeSytle(col,row,4,matrix)

				m5[row] = m5[row] or {}
				m5[row][col] = judgeSytle(col,row,10,matrix)
			end
		end
		return m1,m2,m3,m4,m5
	end


	type1Matrix,type2Matrix,type3Matrix,type4Matrix,type10Matrix
	= createSameNumberMatrix(originMatrix)

	function changeMatrixPoint(_matrix,_row,_col,bAdd)
		local d = bAdd and 1 or -1
		local row,col,matrix = row,col,_matrix
		local args = matrix[row][col]

		if not bAdd then
			for i=args[3][1],args[3][2] do
				if i ~= col then
					local tmpMatrix=matrix[row][i]
					tmpMatrix[1] = tmpMatrix[1] + d 		-- h++
					tmpMatrix[3][1] = tmpMatrix[3][1] - d
				end

			end
		end
	end
	
	local tag1Matrix = {}
	for row=1,9 do
		for col=1,8 do
			tag1Matrix[row] = tag1Matrix[row] or {}
			tag1Matrix[row][col] = {0,0}
		end
	end
	local tag2Matrix = clone(tag1Matrix)
	local tag3Matrix = clone(tag1Matrix)
	local tag4Matrix = clone(tag1Matrix)

	local needChangePoints = {} 						--存储哪些点需要重置的点


	-- 筛选需要重置的点
	for row=1,9 do
		for col=1,8 do
			local sytle = originMatrix[row][col]
			if sytle == 1 then 						
				local h,v = type1Matrix[row][col][1],type1Matrix[row][col][2]
				--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
				if h>=2 or v>=2 then
					if tag1Matrix[row][col][1] == 0 or tag1Matrix[row][col][2] == 0 then
						--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
						local addOver = false		
						if h>=2 and tag1Matrix[row][col][1] == 0 then
							needChangePoints[#needChangePoints+1] = {col,row,sytle}
							addOver = true
							tag1Matrix[row][col][1] = 1
							if col<8 then tag1Matrix[row][col+1][1] = 1 end
							if col<7 then tag1Matrix[row][col+2][1] = 1 end
						end
						if v>=2 and tag1Matrix[row][col][2] == 0 then
							if not addOver then needChangePoints[#needChangePoints+1] = {col,row,sytle} end
							tag1Matrix[row][col][2] = 1
							if row<9 then tag1Matrix[row+1][col][2] = 1 end
							if row<8 then tag1Matrix[row+2][col][2] = 1 end
						end
					end
				end
			elseif sytle == 2 then 						
				local h,v = type2Matrix[row][col][1],type2Matrix[row][col][2]
				--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
				if h>=2 or v>=2 then
					if tag2Matrix[row][col][1] == 0 or tag2Matrix[row][col][2] == 0 then
						--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
						local addOver = false
						if h>=2 and tag2Matrix[row][col][1] == 0 then
							needChangePoints[#needChangePoints+1] = {col,row,sytle}
							addOver = true
							tag2Matrix[row][col][1] = 1
							if col<8 then tag2Matrix[row][col+1][1] = 1 end
							if col<7 then tag2Matrix[row][col+2][1] = 1 end
						end
						if v>=2 and tag2Matrix[row][col][2] == 0 then
							if not addOver then needChangePoints[#needChangePoints+1] = {col,row,sytle} end
							tag2Matrix[row][col][2] = 1
							if row<9 then tag2Matrix[row+1][col][2] = 1 end
							if row<8 then tag2Matrix[row+2][col][2] = 1 end
						end
					end
				end
			elseif sytle == 3 then 						
				local h,v = type3Matrix[row][col][1],type3Matrix[row][col][2]
				--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
				if h>=2 or v>=2 then
					if tag3Matrix[row][col][1] == 0 or tag3Matrix[row][col][2] == 0 then
						--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
						local addOver = false
						if h>=2 and tag3Matrix[row][col][1] == 0 then
							needChangePoints[#needChangePoints+1] = {col,row,sytle}
							addOver = true
							tag3Matrix[row][col][1] = 1
							if col<8 then tag3Matrix[row][col+1][1] = 1 end
							if col<7 then tag3Matrix[row][col+2][1] = 1 end
						end
						if v>=2 and tag3Matrix[row][col][2] == 0 then
							if not addOver then needChangePoints[#needChangePoints+1] = {col,row,sytle} end
							tag3Matrix[row][col][2] = 1
							if row<9 then tag3Matrix[row+1][col][2] = 1 end
							if row<8 then tag3Matrix[row+2][col][2] = 1 end
						end
					end
				end
			elseif sytle == 4 then 						
				local h,v = type4Matrix[row][col][1],type4Matrix[row][col][2]
				--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
				if h>=2 or v>=2 then
					if tag4Matrix[row][col][1] == 0 or tag4Matrix[row][col][2] == 0 then
						--printf('(%d,%d),h=%d,v=%d',col,row,h,v)
						local addOver = false
						if h>=2 and tag4Matrix[row][col][1] == 0 then
							needChangePoints[#needChangePoints+1] = {col,row,sytle}
							addOver = true
							tag4Matrix[row][col][1] = 1
							if col<8 then tag4Matrix[row][col+1][1] = 1 end
							if col<7 then tag4Matrix[row][col+2][1] = 1 end
						end
						if v>=2 and tag4Matrix[row][col][2] == 0 then
							if not addOver then needChangePoints[#needChangePoints+1] = {col,row,sytle} end
							tag4Matrix[row][col][2] = 1
							if row<9 then tag4Matrix[row+1][col][2] = 1 end
							if row<8 then tag4Matrix[row+2][col][2] = 1 end
						end
					end
				end
			end
		end
	end

	--local backPoints1 = clone(needChangePoints) 			--备份筛选的点
	--local backPoints2 = clone(needChangePoints)

	--[[
	for k,v in ipairs(needChangePoints) do
		printf('(%d,%d),%d need be change',v[1],v[2],v[3])
	end
	]]

	local intent = {} 										--存放可以转换的类型
	for k,v in ipairs(needChangePoints) do
		local numberSytle1,mimSytle2,numberSytle3,numberSytle4,sytlenumber

		--获取同一点当前相同类型的最大数量
		numberSytle1 = type1Matrix[v[2]][v[1]][1]>type1Matrix[v[2]][v[1]][2] and type1Matrix[v[2]][v[1]][1] or type1Matrix[v[2]][v[1]][2]
		numberSytle2 = type2Matrix[v[2]][v[1]][1]>type2Matrix[v[2]][v[1]][2] and type2Matrix[v[2]][v[1]][1] or type2Matrix[v[2]][v[1]][2]
		numberSytle3 = type3Matrix[v[2]][v[1]][1]>type3Matrix[v[2]][v[1]][2] and type3Matrix[v[2]][v[1]][1] or type3Matrix[v[2]][v[1]][2]
		numberSytle4 = type4Matrix[v[2]][v[1]][1]>type4Matrix[v[2]][v[1]][2] and type4Matrix[v[2]][v[1]][1] or type4Matrix[v[2]][v[1]][2]
		--printf('(%d,%d),sytle=%d,numberSytle1=%d',v[1],v[2],v[3],numberSytle1)
		--printf('(%d,%d),sytle=%d,numberSytle2=%d',v[1],v[2],v[3],numberSytle2)
		--printf('(%d,%d),sytle=%d,numberSytle3=%d',v[1],v[2],v[3],numberSytle3)
		--printf('(%d,%d),sytle=%d,numberSytle4=%d',v[1],v[2],v[3],numberSytle4)

		local minNumber,tmpNumber
		local args = {}
		args.frome = v
		args.intent = {}

		if v[3] == 1 then
			--[[
			--获取另外三个中比较小的
			tmpNumber = numberSytle2 > numberSytle3 and numberSytle3 or numberSytle2
			minNumber = numberSytle4 > tmpNumber and tmpNumber or numberSytle4
			]]
			
			if numberSytle2 < 2 then
				args.intent[#args.intent+1] = 2
			end
			if numberSytle3 < 2 then
				args.intent[#args.intent+1] = 3
			end
			if numberSytle4 < 2 then
				args.intent[#args.intent+1] = 4
			end
		elseif v[3] == 2 then
			if numberSytle1 < 2 then
				args.intent[#args.intent+1] = 1
			end
			if numberSytle3 < 2 then
				args.intent[#args.intent+1] = 3
			end
			if numberSytle4 < 2 then
				args.intent[#args.intent+1] = 4
			end
		elseif v[3] == 3 then
			--[[
			--获取另外三个中比较小的
			tmpNumber = numberSytle2 > numberSytle3 and numberSytle3 or numberSytle2
			minNumber = numberSytle4 > tmpNumber and tmpNumber or numberSytle4
			]]
			if numberSytle2 < 2 then
				args.intent[#args.intent+1] = 2
			end
			if numberSytle1 < 2 then
				args.intent[#args.intent+1] = 1
			end
			if numberSytle4 < 2 then
				args.intent[#args.intent+1] = 4
			end
		elseif v[3] == 4 then
			--[[
			--获取另外三个中比较小的
			tmpNumber = numberSytle2 > numberSytle3 and numberSytle3 or numberSytle2
			minNumber = numberSytle4 > tmpNumber and tmpNumber or numberSytle4
			]]
			if numberSytle2 < 2 then
				args.intent[#args.intent+1] = 2
			end
			if numberSytle3 < 2 then
				args.intent[#args.intent+1] = 3
			end
			if numberSytle1 < 2 then
				args.intent[#args.intent+1] = 1
			end
		end
		if #args.intent == 0 then return false end 								--如果有点不能更换，则返回错误，通知重新生成矩阵
		intent[#intent+1] = args
	end

	--[[
	for k,v in ipairs(intent) do
		for _,tv in ipairs(v.intent) do
			printf('(%d,%d),can change to %d',v.frome[1],v.frome[2],tv)
		end
	end
	--]]

	
	local backIntent = clone(intent) 			--备份
	for i=1,#backIntent do
		for j=1,#backIntent-1 do
			local tmp = backIntent[j+1]
			if #tmp.intent < #backIntent[j].intent then
				backIntent[j+1] = backIntent[j]
				backIntent[j] = tmp
			end
		end
	end

	--[[
	print('... newly sort intent ...')
	for k,v in ipairs(backIntent) do
		for _,tv in ipairs(v.intent) do
			printf('(%d,%d),can change to %d',v.frome[1],v.frome[2],tv)
		end
	end
	]]

	local tmpThePoints = {}
	for _,v in pairs(backIntent) do
		tmpThePoints[#tmpThePoints+1] = v.frome
	end
	for r=1,9 do
		for c=1,8 do
			if baseMap[r][c] == 0 or baseWall[r][c] == 1 then
				tmpThePoints[#tmpThePoints+1] ={c,r,0} 				--将墙或空位置加入不查找表里
			end
		end
	end


	--判断点是否已经在intent表里
	function isInThePoints(__col,__row,__intent)
		local col,row,intent = __col,__row,__intent
		for _,v in ipairs(intent) do
			if col == v[1] and row == v[2] then return true end
		end
		return false
	end

	local replaceTable = {}
	local type1Replaced = {}
	for i=1,9 do
		type1Replaced[#type1Replaced+1] = {0,0,0,0,0,0,0,0} 				--标记替换点，一个点替换，四周2点内不在可替换
	end
	local type2Replaced = clone(type1Replaced)
	local type3Replaced = clone(type1Replaced)
	local type4Replaced = clone(type1Replaced)

	function canReplace(__col1,__row1,__sytle1,__col2,__row2,__sytle2)
		local col1,row1,sytle1,col2,row2,sytle2 = __col1,__row1,__sytle1,__col2,__row2,__sytle2
		local n1,n2
		if sytle1 == 1 then
			n1 = type1Replaced[row2][col2]
		elseif sytle1 == 2 then
			n1 = type2Replaced[row2][col2]
		elseif sytle1 == 3 then
			n1 = type3Replaced[row2][col2]
		elseif sytle1 == 4 then
			n1 = type4Replaced[row2][col2]
		end
		if sytle2 == 1 then
			n2 = type1Replaced[row1][col1]
		elseif sytle2 == 2 then
			n2 = type2Replaced[row1][col1]
		elseif sytle2 == 3 then
			n2 = type3Replaced[row1][col1]
		elseif sytle2 == 4 then
			n2 = type4Replaced[row1][col1]
		end
		if n2 == 1 or n1 == 1 then 
			return false
		else
			return true
		end
	end
	function toReplace(__col1,__row1,__sytle1,__col2,__row2,__sytle2)
		local col1,row1,sytle1,col2,row2,sytle2 = __col1,__row1,__sytle1,__col2,__row2,__sytle2
		if sytle1 == 1 then
			type1Replaced[row2][col2] = 1
			if row2>1 then type1Replaced[row2-1][col2] = 1 end
			if row2>2 then type1Replaced[row2-2][col2] = 1 end
			if row2<8 then type1Replaced[row2+2][col2] = 1 end
			if row2<9 then type1Replaced[row2+1][col2] = 1 end
			if col2>1 then type1Replaced[row2][col2-1] = 1 end
			if col2>2 then type1Replaced[row2][col2-2] = 1 end
			if col2<8 then type1Replaced[row2][col2+1] = 1 end
			if col2<7 then type1Replaced[row2][col2+2] = 1 end
		elseif sytle1 == 2 then
			type2Replaced[row2][col2] = 1
			if row2>1 then type2Replaced[row2-1][col2] = 1 end
			if row2>2 then type2Replaced[row2-2][col2] = 1 end
			if row2<8 then type2Replaced[row2+2][col2] = 1 end
			if row2<9 then type2Replaced[row2+1][col2] = 1 end
			if col2>1 then type2Replaced[row2][col2-1] = 1 end
			if col2>2 then type2Replaced[row2][col2-2] = 1 end
			if col2<8 then type2Replaced[row2][col2+1] = 1 end
			if col2<7 then type2Replaced[row2][col2+2] = 1 end
		elseif sytle1 == 3 then
			type3Replaced[row2][col2] = 1
			if row2>1 then type3Replaced[row2-1][col2] = 1 end
			if row2>2 then type3Replaced[row2-2][col2] = 1 end
			if row2<8 then type3Replaced[row2+2][col2] = 1 end
			if row2<9 then type3Replaced[row2+1][col2] = 1 end
			if col2>1 then type3Replaced[row2][col2-1] = 1 end
			if col2>2 then type3Replaced[row2][col2-2] = 1 end
			if col2<8 then type3Replaced[row2][col2+1] = 1 end
			if col2<7 then type3Replaced[row2][col2+2] = 1 end
		elseif sytle1 == 4 then
			type4Replaced[row2][col2] = 1
			if row2>1 then type4Replaced[row2-1][col2] = 1 end
			if row2>2 then type4Replaced[row2-2][col2] = 1 end
			if row2<8 then type4Replaced[row2+2][col2] = 1 end
			if row2<9 then type4Replaced[row2+1][col2] = 1 end
			if col2>1 then type4Replaced[row2][col2-1] = 1 end
			if col2>2 then type4Replaced[row2][col2-2] = 1 end
			if col2<8 then type4Replaced[row2][col2+1] = 1 end
			if col2<7 then type4Replaced[row2][col2+2] = 1 end
		end

		if sytle2 == 1 then
			type1Replaced[row1][col1] = 1
			if row1>1 then type1Replaced[row1-1][col1] = 1 end
			if row1>2 then type1Replaced[row1-2][col1] = 1 end
			if row1<8 then type1Replaced[row1+2][col1] = 1 end
			if row1<9 then type1Replaced[row1+1][col1] = 1 end
			if col1>1 then type1Replaced[row1][col1-1] = 1 end
			if col1>2 then type1Replaced[row1][col1-2] = 1 end
			if col1<8 then type1Replaced[row1][col1+1] = 1 end
			if col1<7 then type1Replaced[row1][col1+2] = 1 end
		elseif sytle2 == 2 then
			type2Replaced[row1][col1] = 1
			if row1>1 then type2Replaced[row1-1][col1] = 1 end
			if row1>2 then type2Replaced[row1-2][col1] = 1 end
			if row1<8 then type2Replaced[row1+2][col1] = 1 end
			if row1<9 then type2Replaced[row1+1][col1] = 1 end
			if col1>1 then type2Replaced[row1][col1-1] = 1 end
			if col1>2 then type2Replaced[row1][col1-2] = 1 end
			if col1<8 then type2Replaced[row1][col1+1] = 1 end
			if col1<7 then type2Replaced[row1][col1+2] = 1 end
		elseif sytle2 == 3 then
			type3Replaced[row1][col1] = 1
			if row1>1 then type3Replaced[row1-1][col1] = 1 end
			if row1>2 then type3Replaced[row1-2][col1] = 1 end
			if row1<8 then type3Replaced[row1+2][col1] = 1 end
			if row1<9 then type3Replaced[row1+1][col1] = 1 end
			if col1>1 then type3Replaced[row1][col1-1] = 1 end
			if col1>2 then type3Replaced[row1][col1-2] = 1 end
			if col1<8 then type3Replaced[row1][col1+1] = 1 end
			if col1<7 then type3Replaced[row1][col1+2] = 1 end
		elseif sytle2 == 4 then
			type4Replaced[row1][col1] = 1
			if row1>1 then type4Replaced[row1-1][col1] = 1 end
			if row1>2 then type4Replaced[row1-2][col1] = 1 end
			if row1<8 then type4Replaced[row1+2][col1] = 1 end
			if row1<9 then type4Replaced[row1+1][col1] = 1 end
			if col1>1 then type4Replaced[row1][col1-1] = 1 end
			if col1>2 then type4Replaced[row1][col1-2] = 1 end
			if col1<8 then type4Replaced[row1][col1+1] = 1 end
			if col1<7 then type4Replaced[row1][col1+2] = 1 end
		end
	end


	for _,v in ipairs(backIntent) do
		local isOK = false
		for r=1,9 do
			if isOK then break end
			for c=1,8 do
				if not isInThePoints(c,r,tmpThePoints) then
					for _,iv in ipairs(v.intent) do  		--查找可替换表
						if iv == 1 then 								--意图替换成类型1，判断类型是否可替换
							if type1Matrix[r][c][1]<2 and type1Matrix[r][c][2]<2 then 	--如果可以替换
								if canReplace(v.frome[1],v.frome[2],v.frome[3],c,r,1) then
									isOK = true 											--查找结束
									replaceTable[#replaceTable+1] = {frome=v.frome,to={c,r,1}} --将替换的两个点放入替换表里
									tmpThePoints[#tmpThePoints+1] = {c,r,1} 				--标记被替换点，不在被查找
									toReplace(v.frome[1],v.frome[2],v.frome[3],c,r,1)
								end
							end
						elseif iv == 2 then 								--意图替换成类型1，判断类型是否可替换
							if type2Matrix[r][c][1]<2 and type2Matrix[r][c][2]<2 then 	--如果可以替换
								if canReplace(v.frome[1],v.frome[2],v.frome[3],c,r,2) then
									isOK = true 											--查找结束
									replaceTable[#replaceTable+1] = {frome=v.frome,to={c,r,2}} --将替换的两个点放入替换表里
									tmpThePoints[#tmpThePoints+1] = {c,r,2} 				--标记被替换点，不在被查找
									toReplace(v.frome[1],v.frome[2],v.frome[3],c,r,2)
								end
							end
						elseif iv == 3 then 								--意图替换成类型1，判断类型是否可替换
							if type3Matrix[r][c][1]<2 and type3Matrix[r][c][2]<2 then 	--如果可以替换			
								if canReplace(v.frome[1],v.frome[2],v.frome[3],c,r,3) then
									isOK = true 											--查找结束
									replaceTable[#replaceTable+1] = {frome=v.frome,to={c,r,3}} --将替换的两个点放入替换表里
									tmpThePoints[#tmpThePoints+1] = {c,r,1} 		  			--标记被替换点，不在被查找
									toReplace(v.frome[1],v.frome[2],v.frome[3],c,r,3)
								end
							end
						elseif iv == 4 then 								--意图替换成类型1，判断类型是否可替换
							if type4Matrix[r][c][1]<2 and type4Matrix[r][c][2]<2 then 	--如果可以替换
								if canReplace(v.frome[1],v.frome[2],v.frome[3],c,r,3) then
									isOK = true 											--查找结束
									replaceTable[#replaceTable+1] = {frome=v.frome,to={c,r,3}} --将替换的两个点放入替换表里
									tmpThePoints[#tmpThePoints+1] = {c,r,1} 				--标记被替换点，不在被查找
									toReplace(v.frome[1],v.frome[2],v.frome[3],c,r,3)
								end
							end
						end
						if isOK then break end
					end
				end
				if isOK then break end
			end
		end
		if not isOK then return false end 												--如果没有找到可替换的，则返回错误
	end

	--printf('replaceTable size = %d',#replaceTable)
	for _,v in ipairs(replaceTable) do
		--printf('replace frome (%d,%d) to (%d,%d)',v.frome[1],v.frome[2],v.to[1],v.to[2])
	end

	return replaceTable
end


--[[
查找矩阵内部是否有可消除的点，如果无则返回false，有返回true
]]
function MatrixMap.checkGSpritesCanByDestory(tager)
	local self = tager
	local originMatrix = {}
	local type1Matrix = {}
	local type2Matrix = {}
	local type3Matrix = {}
	local type4Matrix = {}
	local type10Matrix = {}
	
	--生成原矩阵
	function createOriginMatrix() 
		local matrix = {}
		for row=1,9 do
			for col=1,8 do
				gSprites[row] = gSprites[row] or {}
				if baseMap[row][col] == 0 or baseWall[row][col]==1 then
					matrix[row] = matrix[row] or {}
					matrix[row][col] = 0
				elseif gSprites[row][col] then
					matrix[row] = matrix[row] or {}
					matrix[row][col] = gSprites[row][col].sytle
				else
					matrix[row] = matrix[row] or {}
					matrix[row][col] = 0
				end
			end
		end
		return matrix
	end

	originMatrix = createOriginMatrix()

	--判断矩阵
	function judgeSytle(_col,_row,_sytle,_matrix)
		local col,row,sytle = _col,_row,_sytle
		local matrix = _matrix
		local h,v = 0,0
		local hp,vp = {col,col},{row,row}
		--1
		for i=col+1,8 do
			if matrix[row][i] == sytle then
				h = h + 1
				hp[2] = i
			else
				break
			end
		end
		for i=col-1,1,-1 do
			if matrix[row][i] == sytle then
				h = h + 1
				hp[1] = i
			else
				break
			end
		end

		--2
		for i=row+1,9 do
			if matrix[i][col] == sytle then
				v = v + 1
				vp[2] = i
			else
				break
			end
		end
		for i=row-1,1,-1 do
			if matrix[i][col] == sytle then
				v = v + 1
				vp[1] = i
			else
				break
			end
		end
		return {h,v,hp,vp}
	end

	--生成各类形相同数量的矩阵
	function createSameNumberMatrix(_matrix)
		local matrix = _matrix
		local m1,m2,m3,m4,m5 = {},{},{},{},{}

		for row=1,9 do
			for col=1,8 do
				m1[row] = m1[row] or {}
				m1[row][col] = judgeSytle(col,row,1,matrix)

				m2[row] = m2[row] or {}
				m2[row][col] = judgeSytle(col,row,2,matrix)

				m3[row] = m3[row] or {}
				m3[row][col] = judgeSytle(col,row,3,matrix)

				m4[row] = m4[row] or {}
				m4[row][col] = judgeSytle(col,row,4,matrix)

				m5[row] = m5[row] or {}
				m5[row][col] = judgeSytle(col,row,10,matrix)
			end
		end
		return m1,m2,m3,m4,m5
	end


	type1Matrix,type2Matrix,type3Matrix,type4Matrix,type10Matrix
	= createSameNumberMatrix(originMatrix)

	function checkPointCanByDestory(__col,__row,__sytle,dir) 			--判断该点，sytle类型是否可消除,dir显示原始点的方向，0水平，1垂直，即0时h要-1，1时v要-1
		local col,row,sytle = __col,__row,__sytle 						--即要忽略原始的点
		local h,v,hp,vp = 0,0,{},{}
		if sytle == 1 then
			h=type1Matrix[row][col][1]
			v=type1Matrix[row][col][2]
			hp=type1Matrix[row][col][3]
			vp=type1Matrix[row][col][4]
		elseif sytle == 2 then
			h=type2Matrix[row][col][1]
			v=type2Matrix[row][col][2]
			hp=type2Matrix[row][col][3]
			vp=type2Matrix[row][col][4]
		elseif sytle == 3 then
			h=type3Matrix[row][col][1]
			v=type3Matrix[row][col][2]
			hp=type3Matrix[row][col][3]
			vp=type3Matrix[row][col][4]
		elseif sytle == 4 then
			h=type4Matrix[row][col][1]
			v=type4Matrix[row][col][2]
			hp=type4Matrix[row][col][3]
			vp=type4Matrix[row][col][4]
		end
		if dir == 0 then
			h = h - 1
		else
			v = v - 1
		end
		if h>=2 or v>=2 then 
			--printf('h=%d,v=%d,(%d,%d)',h,v,col,row)
			return true,h,v,hp,vp
		else return false
		end
	end

	local canDestoryPoints = {}
	for row=1,9 do
		for col=1,8 do
			if baseMap[row][col]==1 or baseWall[row][col] == 0 then
				local sytle = originMatrix[row][col] 												--当前点类型
				if col-1>0 then 																	--判断左边点
					local result,h,v,hp,vp = checkPointCanByDestory(col-1,row,sytle,0)
					if result then
						local points = {}
						points[#points+1] = {col,row}
						if h>=2 then
							for i=hp[1],hp[2] do
								if i~=col-1 then
									points[#points+1] = {i,row}
								end
							end
						end
						if v>=2 then
							for i=vp[1],vp[2] do
								if i~=row then
									points[#points+1] = {col-1,i}
								end
							end
						end
						canDestoryPoints[#canDestoryPoints+1] = points
					end
				end
				if col+1<9 then 																--判断右边点
					local result,h,v,hp,vp = checkPointCanByDestory(col+1,row,sytle,0)
					if result then
						local points = {}
						points[#points+1] = {col,row}
						if h>=2 then
							for i=hp[1],hp[2] do
								if i~=col+1 then
									points[#points+1] = {i,row}
								end
							end
						end
						if v>=2 then
							for i=vp[1],vp[2] do
								if i~=row then
									points[#points+1] = {col+1,i}
								end
							end
						end
						canDestoryPoints[#canDestoryPoints+1] = points
					end
				end
				if row-1>0 then 																--判断下边点
					local result,h,v,hp,vp = checkPointCanByDestory(col,row-1,sytle,1)
					if result then
						local points = {}
						points[#points+1] = {col,row}
						if h>=2 then
							for i=hp[1],hp[2] do
								if i~=col then
									points[#points+1] = {i,row-1}
								end
							end
						end
						if v>=2 then
							for i=vp[1],vp[2] do
								if i~=row-1 then
									points[#points+1] = {col,i}
								end
							end
						end
						canDestoryPoints[#canDestoryPoints+1] = points
					end
				end
				if row+1<10 then 																--判断上边点
					local result,h,v,hp,vp = checkPointCanByDestory(col,row+1,sytle,1)
					if result then
						local points = {}
						points[#points+1] = {col,row}
						if h>=2 then
							for i=hp[1],hp[2] do
								if i~=col then
									points[#points+1] = {i,row+1}
								end
							end
						end
						if v>=2 then
							for i=vp[1],vp[2] do
								if i~=row+1 then
									points[#points+1] = {col,i}
								end
							end
						end
						canDestoryPoints[#canDestoryPoints+1] = points
					end
				end
			end
		end
	end
	if #canDestoryPoints == 0 then return false
	else return true,canDestoryPoints
	end
end

--[[
随机重置矩阵
]]
function MatrixMap.reRandomGSprites(tager)
	local self = tager
	local willRandomSprites = {} 										--保存有特效的点
	for row=1,9 do
		for col=1,8 do
			if baseMap[row][col]==1 and baseWall[row][col]==0 then
				gSprites[row] = gSprites[row] or {}
				if gSprites[row][col] then
					if gSprites[row][col].effect then
						willRandomSprites[#willRandomSprites+1] = gSprites[row][col]
					else
						gSprites[row][col]:removeSelf() 								--非特效的点，直接删除
					end
				end
			end
		end
	end

	local placeSpritesNumber = #willRandomSprites  						--一共要存放精灵的数量
 	--printf('willRandomSprites size=%d row=%d,col=%d',placeSpritesNumber,willRandomSprites[1].row,willRandomSprites[1].col)
	--随机存放特效点
	local placedPoints = {}
	for i=1,9 do
		placedPoints[#placedPoints+1] = {0,0,0,0,0,0,0,0}
	end

	local newSprites = {}
	function placeEffectSprites()
		for row=1,9 do
			if placeSpritesNumber<1 then break end 														
			for col=1,8 do
				if placeSpritesNumber<1 then break end
				if baseMap[row][col]==1 and baseWall[row][col]==0 and placedPoints[row][col]==0 then
					local toPlace = math.random(1,4) 														--遍历矩阵，1/3的概率存放进特效精灵	
					if toPlace == 1 then
						newSprites[row] = newSprites[row] or {}
						newSprites[row][col] = willRandomSprites[placeSpritesNumber]   						--将最后位置需要存放的精灵加入矩阵
						willRandomSprites[placeSpritesNumber].row = row
						willRandomSprites[placeSpritesNumber].col = col 									--更新坐标
						local x,y = BaseSprite.toPosition(col,row)
						willRandomSprites[placeSpritesNumber]:pos(x,y)
						placeSpritesNumber = placeSpritesNumber - 1 										--放入后，该位置不在需要放入
						placedPoints[row][col] = 1 														--标记该位置已经有东西了，不在放入东西
					end
				end
			end
		end
	end

	while placeSpritesNumber > 0 do
		placeEffectSprites() 									--遍历几次，直到所有精灵都已经放入
	end

	--重新随机生成剩下的矩阵
	for row=1,9 do
		for col=1,8 do
			if baseMap[row][col] == 1 and baseWall[row][col] == 0 and placedPoints[row][col]==0 then 									--随机精灵
				local file = SPRITE_SOURCES[math.random(1,5)]
				local tmpSprite = BaseSprite.new({filename=file,row=row,column=col}):addTo(self)
				tmpSprite:setScale(0.5)
				newSprites[row] = newSprites[row] or {}
				newSprites[row][col] = tmpSprite
				tmpSprite:retain()
			end
		end
	end

	gSprites = newSprites
end

--[[
判断矩阵是否有可消除的，如果有则修正矩阵，并继续判断、修正
]]
function MatrixMap.doReviseGSprites(tager,unReplace)


	local replaceTable
	if gMaxtrixByRandomNumber>20 then  											--如果50次了还无法得到正确的矩阵，则重置矩阵
			MatrixMap.reRandomGSprites(tager)
			gMaxtrixByRandomNumber = 0
			if not MatrixMap.checkGSprites(tager) then 							--如果新的矩阵也需要修复
				replaceTable = MatrixMap.reviseRandomGSprites(tager)             --继续判断
			else
				replaceTable = true
			end
	else
		replaceTable = MatrixMap.reviseRandomGSprites(tager) 			--
	end
	
	while not replaceTable do
		---[[
		if gMaxtrixByRandomNumber>20 then  										--如果50次了还无法得到正确的矩阵，则重置矩阵
			MatrixMap.reRandomGSprites(tager)
			gMaxtrixByRandomNumber = 0
		end
		--]]
		
		if not MatrixMap.checkGSprites(tager) then 							--如果新的矩阵也需要修复
			replaceTable = MatrixMap.reviseRandomGSprites(tager)             --继续判断
		else
			replaceTable = true
		end
	end

	if type(replaceTable) == 'table' then 
		--替换表
		for _,v in ipairs(replaceTable) do
			local fcol,frow = v.frome[1],v.frome[2]
			local tcol,trow = v.to[1],v.to[2]
			local tmpSprite = gSprites[frow][fcol]
			gSprites[frow][fcol] = gSprites[trow][tcol]
			gSprites[trow][tcol] = tmpSprite
			gSprites[frow][fcol].row = frow
			gSprites[frow][fcol].col = fcol
			gSprites[trow][tcol].row = trow
			gSprites[trow][tcol].col = tcol
			--gSprites[trow][tcol]:setScale(0.3)
			--gSprites[frow][fcol]:setScale(0.6)
			---[[
			if not unReplace then
				local x,y = BaseSprite.toPosition(fcol,frow)
				gSprites[frow][fcol]:pos(x,y)
				x,y = BaseSprite.toPosition(tcol,trow)
				gSprites[trow][tcol]:pos(x,y)
			end
			--]]
		end
		---[[
		if not MatrixMap.checkGSprites(tager) then 							--如果新的矩阵也需要修复
			--replaceTable = MatrixMap.reviseRandomGSprites(tager)             --继续判断
			MatrixMap.doReviseGSprites(tager,unReplace)
			return true
		else
			replaceTable = true
		end
		--]]
	else
		return replaceTable
	end

end